package com.shuorikeji.huohuo.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.android.material.imageview.ShapeableImageView;
import com.shuorikeji.huohuo.R;
import com.shuorikeji.huohuo.bean.MhInfoBean;
import com.shuorikeji.huohuo.callback.ClickCallBack;
import com.shuorikeji.huohuo.common.Constant;
import com.shuorikeji.huohuo.utils.BusinessUtils;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.RecyclerView;

public class MrtjMhAdapter extends RecyclerView.Adapter<MrtjMhAdapter.BaseViewHolder> {
    private final Context mContext;
    private List<MhInfoBean> mData = new ArrayList<>();
    private ClickCallBack callBack;

    public MrtjMhAdapter(Context context, ClickCallBack callBackFlag) {
        this.mContext = context;
        this.callBack = callBackFlag;
    }

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.mrtj_mh_item_layout, parent, false);
        BaseViewHolder holder = new BaseViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(BaseViewHolder holder, @SuppressLint("RecyclerView") int position) {
        BusinessUtils.setLoadImg(mContext, Constant.IMG_OSS_BASE_URL + mData.get(position).getCover_image(), holder.sivFm);
        holder.tvTitle.setText(mData.get(position).getName());
        holder.tvZz.setText(mData.get(position).getAuthor());
        holder.tvContent.setText(mData.get(position).getContent());

        holder.llTag.removeAllViews();
        for (int i = 0; i < mData.get(position).getTag_id().size(); i++) {
            View subView = View.inflate(mContext, R.layout.mrtj_tag_item_layout, null);
            subView.setPadding(0, 0, 40, 0);
            TextView tvTagName = subView.findViewById(R.id.tv_tag);
            tvTagName.setText(mData.get(position).getTag_id().get(i));
            holder.llTag.addView(subView);
        }

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (callBack != null) {
                    callBack.onClick(1, mData.get(position));
                }
            }
        });
    }

    public void setData(List<MhInfoBean> data) {
        if (mData != null) {
            mData.clear();
            mData.addAll(data);
            notifyDataSetChanged();
        }
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    class BaseViewHolder extends RecyclerView.ViewHolder {
        ShapeableImageView sivFm;
        TextView tvTitle;
        TextView tvZz;
        LinearLayout llTag;
        TextView tvContent;
        TextView tvGxZj;
        TextView tvScs;

        public BaseViewHolder(View itemView) {
            super(itemView);
            sivFm = itemView.findViewById(R.id.siv_fm);
            tvTitle = itemView.findViewById(R.id.tv_title);
            tvZz = itemView.findViewById(R.id.tv_zz);
            llTag = itemView.findViewById(R.id.ll_tag);
            tvContent = itemView.findViewById(R.id.tv_content);
            tvGxZj = itemView.findViewById(R.id.tv_gx_zj);
            tvScs = itemView.findViewById(R.id.tv_scs);
        }
    }
}
